package com.solution.admin.customer.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.solution.admin.customer.entity.CustomerExternalEntity;
import com.solution.admin.customer.service.CustomerExternalService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;

/**
 * 客户信息扩展表web控制层
 * @author solin
 * @date 2017-09-30 14:10:53
 */
@RequestMapping("/customerExternal")
@Controller
public class CustomerExternalController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(CustomerExternalController.class);

	@Autowired
	private CustomerExternalService customerExternalService;
	
	/**
	 * 跳转客户信息扩展表查询页面
	 * @param CustomerExternal 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-09-30 14:10:53
	 */
	@RequestMapping("/toListCustomerExternalPage")
	public String toListCustomerExternalPage(CustomerExternalEntity customerExternal, HttpServletRequest request) {
		return "customer/customerExternal_list";
	}

	/**
	 * 分页查询客户信息扩展表列表
	 * @param customerExternal 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-09-30 14:10:53
	 */
	@RequestMapping("/listCustomerExternalPage")
	@ResponseBody
	public JSONGrid listCustomerExternalPage(CustomerExternalEntity customerExternal, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("customerId", request.getParameter("customerId"));
		try {
			PageBean pageBean = customerExternalService.listCustomerExternalPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询客户信息扩展表失败", logger, e);
		}
		return new JSONGrid();
	}
	
	/**
	 * 查询客户信息扩展表列表
	 * @param customerExternal
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年10月9日 下午7:56:00
	 */
	@RequestMapping("/listCustomerExternal")
	@ResponseBody
	public List<CustomerExternalEntity> listCustomerExternal(CustomerExternalEntity customerExternal, HttpServletRequest request){
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("customerId", customerExternal.getCustomerId());
		try {
			List<CustomerExternalEntity> list = customerExternalService.listCustomerExternal(paramMap);
			return list;
		} catch (Exception e) {
			handleException("分页查询客户信息扩展表失败", logger, e);
		}
		return new ArrayList<CustomerExternalEntity>();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-09-30 14:10:53
	 */
	@RequestMapping("/toAddCustomerExternal")
	public String toAddCustomerExternal(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "customer/customerExternal_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-09-30 14:10:53
	 */
	@RequestMapping("/toUpdateCustomerExternal")
	public String toUpdateCustomerExternal(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			CustomerExternalEntity customerExternal = customerExternalService.getCustomerExternalById(id);
			request.setAttribute("customerExternal", customerExternal);
		} catch (Exception e) {
			handleException("跳转编辑客户信息扩展表页面错误", logger, e);
		}
		return "customer/customerExternal_form";
	}


	/**
	 * 新增或保存客户信息扩展表
	 * @param request 请求对象
	 * @param customerExternalEntity 客户信息扩展表实体
	 * @return
	 * @author solin
 	 * @date 2017-09-30 14:10:53
	 */
	@RequestMapping("/saveOrUpdateCustomerExternal")
	@ResponseBody
	public Map<String, Object> saveCustomerExternal(HttpServletRequest request, Long customerId, String options) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		String msg = "";
		try {
			/*String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = customerExternalService.updateCustomerExternal(customerExternalEntity);
			}else{
				result = (int)customerExternalService.saveCustomerExternal(customerExternalEntity);
			}*/
			List<HashMap> optionList = JSONArray.parseArray(options, HashMap.class);
			List<CustomerExternalEntity> customerExternalList = new ArrayList<CustomerExternalEntity>();
			customerExternalService.deleteCustomerExternalByCustomerId(customerId);
			for (int i = 0; i < optionList.size(); i++) {
				CustomerExternalEntity optionEntity = new CustomerExternalEntity();
				optionEntity.setCustomerId(customerId);
				optionEntity.setFieldId(Long.parseLong(optionList.get(i).get("fieldId").toString()));
				optionEntity.setFieldValue(optionList.get(i).get("fieldValue").toString());
				customerExternalList.add(optionEntity);
			}
			result = customerExternalService.batchInsertCustomerExternal(customerExternalList);
			if (result > 1) {
				result = 1;
				msg = "保存客户扩展信息成功";
			}
		} catch (Exception e) {
			msg = "保存客户信息扩展表失败";
			handleException(msg, logger, e);
		}
		
		resultMap.put("code", result);
		resultMap.put("msg", msg);
		return resultMap;
	}
	
}
